﻿using System.Collections;
using System.Threading.Tasks;
using UnityEngine;
using DG.Tweening;
using Cysharp.Threading.Tasks;
using System.Collections.Generic;
using UnityEngine.UI;

public class ScreenSplashPanel : UIPanelBase
{
    [SerializeField]
    private List<CanvasGroup> m_Groups;
    [SerializeField]
    private Button m_SkipButton;

    public bool tweening { get; private set; } = false;

    private Sequence m_CurrentSequence;
    private bool m_Skippable = false;

    public override void OnInit()
    {
        m_SkipButton.onClick.AddListener(OnSkipButton);
    }

    private void OnSkipButton()
    {
        if (m_Skippable)
        {
            m_CurrentSequence?.Kill();
        }
    }

    public async UniTask SplashAsync()
    {
        tweening = true;
        for (int i = 0; i < m_Groups.Count; i++)
        {
            m_Groups[i].alpha = 0f;
        }
        for (int i = 0; i < m_Groups.Count; i++)
        {
            var group = m_Groups[i];
            m_Skippable = false;
            var showSequence = DOTween.Sequence();
            showSequence.AppendInterval(0.5f);
            showSequence.Append(group.DOFade(1f, 1f).OnStart(() => group.alpha = 0f).OnComplete(() => group.alpha = 1f));
            await showSequence.Play().AsyncWaitForCompletion();

            //可跳过内容
            m_Skippable = true;
            m_CurrentSequence?.Kill();
            m_CurrentSequence = DOTween.Sequence();
            m_CurrentSequence.AppendInterval(3f);
            await m_CurrentSequence.Play().AsyncWaitForCompletion();
            m_Skippable = false;

            var hideSequence = DOTween.Sequence();
            hideSequence.Append(group.DOFade(0f, 1f).OnStart(() => group.alpha = 1f).OnComplete(() => group.alpha = 0f));
            hideSequence.AppendInterval(0.5f);
            await hideSequence.Play().AsyncWaitForCompletion();
        }
        tweening = false;
        Hide();
    }
}